package com.wang.decagency.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.decagency.entity.GCost;
import com.wang.decagency.entity.GDocument;
import com.wang.decagency.entity.SysCustomer;
import com.wang.decagency.service.impl.GCostServiceImpl;
import com.wang.decagency.service.impl.GDocumentServiceImpl;
import com.wang.utils.ResultEntity;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wang
 * @since 2023-05-29
 */
@Slf4j
@RestController
@Api(tags = "单证管理")
@RequestMapping("/decagency/document")
public class GDocumentController {

    @Autowired
    GDocumentServiceImpl documentService;

    @PostMapping("/getDocumentList")
    @ApiOperation(value = "查询单证列表", notes = "查询单证列表")
    @ApiResponses({@ApiResponse(code = 2000, message = "成功")})
    public Object getDocumentList(@RequestBody Map<String, Object> mapParam) throws InvocationTargetException, IllegalAccessException {
        GDocument document = JSON.parseObject(JSON.toJSONString(mapParam), GDocument.class);
        log.info("document:"+JSON.toJSONString(document));
        // 从params中获取分页参数
        int pageNum = mapParam.get("pageNum")!=null ? (int) mapParam.get("pageNum"):1 ;
        int pageSize =mapParam.get("pageSize")!=null ? (int) mapParam.get("pageSize"):5 ;

        log.info(JSON.toJSONString("pageNum:"+pageNum));
        QueryWrapper<GDocument> queryWrapper = new QueryWrapper<>(document);
        Page<GDocument> page = new Page<>(pageNum, pageSize);
        IPage<GDocument> SysUserList = documentService.page(page, queryWrapper);
        return ResultEntity.ok(SysUserList);
    }

    @PostMapping("/addDocument")
    @ApiOperation(value = "添加单证", notes = "添加单证")
    public Object addDocument(@RequestBody @ApiParam(name = "customer", value = "单证信息", required = true) GDocument customer) {
        if (Objects.isNull(customer)  || customer.getFiles() == null) {
            return ResultEntity.fail("入参不全");
        }
        documentService.saveOrUpdate(customer);
        return ResultEntity.ok();
    }

    @PostMapping("/getByKey")
    @ApiOperation(value = "查询详情", notes = "查询详情")
    @ApiResponses({@ApiResponse(code = 2000, message = "成功")})
    public Object getByKey(@RequestBody @ApiParam(name = "cost", value = "单证信息") GDocument obj) throws Exception {
        obj = documentService.getById(obj.getDocumentId());
        return ResultEntity.ok(obj);
    }

    @PostMapping("/delByKeys")
    @ApiOperation(value = "根据主键删除", notes = "根据主键删除")
    @ApiResponses({@ApiResponse(code = 2000, message = "成功")})
    public Object delByKeys(@RequestBody List<Integer> ids) throws Exception {
        boolean b = documentService.removeByIds(ids);
        return ResultEntity.ok(b);
    }


}
